import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class ImageUtils {
  /**
   * 选择图片
   */
  static Future<File> getImage({ImageSource source, int quality = 100}) async {
    XFile file = await ImagePicker().pickImage(source: source, imageQuality: quality);
    if (file == null) {
      return null;
    } else {
      return File(file.path);
    }
  }

  static Future<File> chosePhotoOnAlbumOrCamera({BuildContext context}) async {
    File file;
    await showModalBottomSheet(
        context: context,
        backgroundColor: Colors.transparent,
        builder: (context) {
          return Container(
            decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.vertical(top: Radius.circular(10))),
            width: MediaQuery.of(context).size.width,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                InkWell(
                    onTap: () async {
                      file = await getImage(source: ImageSource.camera);
                      Navigator.pop(context);
                    },
                    child: Container(width: double.infinity, alignment: Alignment.center,padding: EdgeInsets.symmetric(vertical: 15), child: Text("拍照"))),
                Divider(height: 1),
                InkWell(
                    onTap: () async {
                      file = await getImage(source: ImageSource.gallery);
                      Navigator.pop(context);
                    },
                    child: Container(width: double.infinity, alignment: Alignment.center,padding: EdgeInsets.symmetric(vertical: 15), child: Text("相册"))),
                Divider(height: 1, color: Colors.black),
                InkWell(
                  onTap: () {
                    print("取消");
                    Navigator.pop(context);
                  },
                  child: Container(width: double.infinity, alignment: Alignment.center, padding: EdgeInsets.symmetric(vertical: 15), child: Text("取消")),
                ),
              ],
            ),
          );
        });
    return file;
  }
}
